package com.zhbcm.outupload.web;

import com.zhbcm.outupload.common.response.ApiResult;
import com.zhbcm.outupload.common.response.ResultUtil;
import com.zhbcm.outupload.entity.MultipartFileParam;
import com.zhbcm.outupload.utils.UploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2021/6/26 20:25
 */
@RestController
public class FileController {
    @Autowired
    private UploadFileUtil uploadFileUtil;

    @PostMapping("/upload")
    public String upload(MultipartFile file) {
        return uploadFileUtil.upload(file);
    }

    @GetMapping("/")
    public void download(String url, HttpServletResponse res) {
        uploadFileUtil.downloadImg(url, res);
    }

    @GetMapping("/checkMd5")
    public ApiResult checkMd5(String md5) {
        Map<String, Object> map = uploadFileUtil.checkMd5(md5);
        return ResultUtil.success(map);
    }

    @PostMapping(value = "/chunkUpload")
    public ApiResult chunkUpload(MultipartFileParam multipartFileParam) {
        return uploadFileUtil.uploadAppendFile(multipartFileParam);
    }
}
